﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors;
using System.Drawing;
using System.Windows.Forms;
using ProgNetComponentsDx.Browsers;
using ProgNetComponentsDx.Enums;

namespace ProgNetComponentsDx.Design.Painters
{
    internal class LookUpEditCustomPainter : DevExpress.XtraEditors.Popup.PopupLookUpFormPainter
    {
        public LookUpEditCustomPainter()
        {
        }
        public Browser Browser
        {
            get;
            set;
        }
        private bool IsCheckEdit(LookUpColumnHeaderObjectInfoArgs header)
        {
            bool res = false;
            if (Browser != null && header != null && Browser.Columns[header.Column.FieldName] != null &&
               Browser.Columns[header.Column.FieldName].ColumnType == BrowserColumnType.CheckEdit)
            {
                res = true;
            }
            return res;
        }
        protected override void DrawRow(PopupFormGraphicsInfoArgs gi, LookUpRowInfo row, PopupLookUpEditFormViewInfo vi)
        {
            if (Browser != null)
            {
                Graphics g = gi.Graphics;
                bool bCheckBox = false;
                for (int i = 0; i < vi.Headers.Count; i++)
                {
                    LookUpColumnHeaderObjectInfoArgs header = (LookUpColumnHeaderObjectInfoArgs)vi.Headers[i];
                    if (IsCheckEdit(header))
                    {
                        bCheckBox = true;
                        break;
                    }
                }
                if (bCheckBox)
                {
                    Brush textBrush = vi.PaintAppearanceContent.GetForeBrush(gi.Cache);
                    if (row.RowIndex == vi.SelectedIndex)
                    {
                        g.FillRectangle(SystemBrushes.Highlight, row.Bounds);
                        textBrush = SystemBrushes.HighlightText;
                    }
                    for (int i = 0; i < vi.Headers.Count; i++)
                    {
                        LookUpColumnHeaderObjectInfoArgs header = (LookUpColumnHeaderObjectInfoArgs)vi.Headers[i];
                        Rectangle headerBounds = header.Bounds;
                        Rectangle stringRect = new Rectangle(headerBounds.Left + 2, row.Bounds.Top, headerBounds.Width - 4, row.Bounds.Height);
                        Rectangle checkboxRect = new Rectangle(headerBounds.Left + 10, row.Bounds.Top + 3, headerBounds.Width - 10, row.Bounds.Height - 3);
                        vi.CellFormat.Alignment = header.Alignment;

                        string cellString = row.RecordStrings[i];
                        if (IsCheckEdit(header))
                        {
                            if (cellString == Browser.Columns[header.Column.FieldName].ValueChecked.AsString())
                            {
                                DevExpress.Utils.Paint.XPaint.Graphics.DrawCheckBox(g, checkboxRect, ButtonState.Checked);
                            }
                            else if (cellString == Browser.Columns[header.Column.FieldName].ValueUnchecked.AsString())
                            {
                                DevExpress.Utils.Paint.XPaint.Graphics.DrawCheckBox(g, checkboxRect, ButtonState.Normal);
                            }
                            else
                                DevExpress.Utils.Paint.XPaint.Graphics.DrawCheckBox(g, checkboxRect, ButtonState.Inactive);
                        }
                        else
                        {
                            DevExpress.Utils.Paint.XPaint.Graphics.DrawString(gi.Cache, cellString, vi.PaintAppearanceContent.Font,
                                textBrush, stringRect, vi.CellFormat);
                        }
                    }
                }
                else
                    base.DrawRow(gi, row, vi);
            }
            else     
                base.DrawRow(gi, row, vi);

        }
    }
}
